﻿#region Using Directives

using System.ComponentModel;
using System.IO;
using EnvDTE;
using FeatureBuilder.ContribLib.Helpers;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;
using VSLangProj;
using VsWebSite;

#endregion

namespace FeatureBuilder.ContribLib.Commands
{
    /// <summary>
    /// Feature Extension Command that does adds an assembly reference to a Project.
    /// </summary>
    [DisplayName("Add Assembly Reference")]
    [Description("Adds an assembly reference to a Project.")]
    [Category("IDE")]
    public class AddAssemblyReference : FeatureCommand
    {
        public Project ReferringProject { get; set; }
        public string AssemblyFilePath { get; set; }

        #region Overrides

        public override void Execute()
        {
            try
            {
                if (DteHelper.IsWebProject(ReferringProject))
                {
                    // This is a web project
                    VSWebSite webProject = ReferringProject.Object as VSWebSite;
                    if (webProject != null)
                    {
                        if (File.Exists(AssemblyFilePath))
                        {
                            webProject.References.AddFromFile(AssemblyFilePath);
                        }
                        else
                        {
                            webProject.References.AddFromGAC(AssemblyFilePath);
                        }
                    }
                }
                else
                {
                    // This is a standard project
                    VSProject vsProject = ReferringProject.Object as VSProject;
                    if (vsProject != null)
                    {
                        vsProject.References.Add(AssemblyFilePath);
                    }
                }
            }
            catch { }
        }

        #endregion
    }
}
